﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dzs.Web.Admin.tempskd
{
    public partial class mylist : Web.Admin.BasePage
    {
        dzs.BLL.tempskd bll = new dzs.BLL.tempskd();
        dzs.BLL.user bu = new BLL.user();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            
        }

        #region gridView

        public void BindData()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append(" and uid=" + getid());
            if (txtKeyword.Text.Trim() != "")
            {
#warning 代码生成警告：请修改 keywordField 为需要匹配查询的真实字段名称
                strWhere.AppendFormat(" and material like '%{0}%'", txtKeyword.Text.Trim());
            }
            ds = bll.GetList(strWhere.ToString());
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string[] GetSelIDlist()
        {
            string[] idlist = {"",""};
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist[0] += gridView.DataKeys[i].Values[0].ToString() + ",";
                        idlist[1] += gridView.DataKeys[i].Values[1].ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist[0] = idlist[0].Substring(0, idlist[0].LastIndexOf(","));
                idlist[1] = idlist[1].Substring(0, idlist[1].LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string[] idlist = GetSelIDlist();
            if (idlist[0].Trim().Length == 0)
                return;
            string r = ddlq.SelectedItem.Text;
            bll.setstate(idlist[0], r);
            if (idlist[1].Trim().Length == 0)
                return;
            r = ddlzl.SelectedItem.Text;

            DateTime d=DateTime.Now;
            if (txtdate.Text != "")
                d = DateTime.Parse(txtdate.Text);
            Maticsoft.BLL.monthSupplayPlan mb = new Maticsoft.BLL.monthSupplayPlan();
            mb.Updateskd(idlist[1], r, d);            

            BindData();
        }
        protected void btnOut_Click(object sender, EventArgs e)
        {
            DateTime dt = System.DateTime.Now;
            string str = dt.ToString("yyyyMMddhhmmss");
            str = str + ".xls";
            gridView.AllowPaging = false;
            //以下为要隐藏的列
            gridView.Columns[0].Visible = false;
            //gridView.Columns[12].Visible = false;
            BindData();
            GridViewToExcel(gridView, "application/ms-excel", str);
            gridView.AllowPaging = true;
            gridView.Columns[0].Visible = true;
            //gridView.Columns[12].Visible = true;
            BindData();

        }
        public static void GridViewToExcel(Control ctrl, string FileType, string FileName)
        {
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
            HttpContext.Current.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
            HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword 
            ctrl.Page.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctrl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }
        /// <summary>
        /// ReLoad this VerifyRenderingInServerForm is neccessary
        /// </summary>
        /// <param name="control"></param>
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
    }
}